package com.qf.shiro2202day02.user.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/order")
public class OrderController {

    @GetMapping("/manage")
    public String manager(){

        final Subject subject = SecurityUtils.getSubject();

        // 检查当前用户是否拥有admin角色
        if (subject.hasRole("admin")) {
            // 检查当前登录用户是否拥有 order:manage 权限
            if (subject.isPermitted("order:manage")) {
                System.out.println("当前用户有admin角色");
                return "redirect:/order.html";
            }else {
                System.out.println("当前用户没有admin角色");
                return "redirect:/error.html";
            }

        }else {
            System.out.println("当前用户没有admin角色");
            return "redirect:/error.html";
        }

    }

    @RequiresRoles(value={"admin","user"})//用来判断角色  同时具有 admin user
    @RequiresPermissions("order:save") //用来判断权限字符串
    @RequestMapping("/save")
    public String save(){
        System.out.println("进入方法");
        return "redirect:/order.html";
    }

}
